package cn.edu.ctbu.datajpa01.service.impl;

import cn.edu.ctbu.datajpa01.domain.Teacher;
import cn.edu.ctbu.datajpa01.domain.repository.TeacherRepository;
import cn.edu.ctbu.datajpa01.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherImpIService implements TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;

    public List<Teacher> findAll(){

        return teacherRepository.findAll();
    }

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Pageable pageable){

        return teacherRepository.findAll(pageable);

    }

    public Page<Teacher> findAll(Example<Teacher> teacher, Pageable pageable){

        return teacherRepository.findAll(teacher,pageable);

    }

    //按id进行查询 @param id 主键，整数
    //@return
    public Teacher findById(Integer id){

        return teacherRepository.findById(id).orElse( null);

    }

    public List<Teacher> findByTname(String tname){


        return teacherRepository.findByTnameLike(tname);
    }


    public Teacher getById(Integer id){

        Teacher teacher=teacherRepository.findById(id).orElse(null);
        return teacher;
    }

    public Teacher insert(Teacher teacher){

        teacherRepository.save(teacher);
        return teacher;
    }

    public Teacher update(Teacher teacher){

        teacherRepository.save(teacher);
        return teacher;
    }

    public void delete(Teacher teacher){

        teacherRepository.delete(teacher);

    }

    public void delete(Integer id){

        Teacher teacher=new Teacher();
        teacher.setId(id);
        teacherRepository.delete(teacher);

    }


    public List<Teacher> findByTnoAndPassword(String tno, String password) {

        return teacherRepository.findByTnoAndPassword(tno,password);

    }
}
